package com.gimi.test_jpa.com.gimi.util;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class BaseResponse<T> {
    public static final int SUCCESS_CODE = 200;
    public static final int FAIL_CODE = 500;
    private String responseTime;
    private int responseCode;
    private String message;
    private String exception;
    private T body;
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public Boolean isSuccess() {
        return (this.responseCode == 200) ? Boolean.valueOf(true) : Boolean.valueOf(false);
    }


    public BaseResponse<T> success() {
        this.responseTime = dateFormat.format(new Date());
        this.responseCode = 200;
        return this;
    }

    public BaseResponse<T> success(T body) {
        this.responseTime = dateFormat.format(new Date());
        this.responseCode = 200;
        this.body = body;
        return this;
    }

    public BaseResponse<T> fail() {
        this.responseTime = dateFormat.format(new Date());
        this.responseCode = 500;
        return this;
    }

    public BaseResponse<T> fail(String message) {
        this.message = message;
        this.responseTime = dateFormat.format(new Date());
        this.responseCode = 500;
        return this;
    }

    public BaseResponse<T> fail(Exception e) {
        this.responseTime = dateFormat.format(new Date());
        this.responseCode = 500;
        if (e != null) {
            this.message = e.getMessage();
            this.exception = e.getClass().getName();
        }
        return this;
    }


    public String getResponseTime() {
        return this.responseTime;
    }


    public void setResponseTime(String responseTime) {
        this.responseTime = responseTime;
    }


    public int getResponseCode() {
        return this.responseCode;
    }


    public void setResponseCode(int responseCode) {
        this.responseCode = responseCode;
    }


    public String getMessage() {
        return this.message;
    }


    public void setMessage(String message) {
        this.message = message;
    }


    public T getBody() {
        return this.body;
    }


    public void setBody(T body) {
        this.body = body;
    }


    public String getException() {
        return this.exception;
    }


    public void setException(String exception) {
        this.exception = exception;
    }
}

